<script setup lang="ts">
import { useRouter } from "vue-router";
import { ResizeBox } from "@arco-design/web-vue";
import { onMounted, reactive, shallowRef } from "vue";
import { getDemoList, getChartList } from "@/api/datalist";
import type Node from "element-plus/es/components/tree/src/model/node";
import { isArray } from "@pureadmin/utils";

defineOptions({
  name: "Empty"
});
interface Tree {
  name: string;
  code?: string;
  children?: Tree[];
}

interface ChartType {
  date: string[];
  fields: string[];
  [key: string]: string[];
}
const state = reactive({
  width: 300,
  active: "first",
  nav: "first"
});

const treeData = shallowRef<Tree[]>([]);
const chartData = shallowRef<ChartType>();
const defaultProps = {
  children: "children",
  label: "name",
  isLeaf: data => {
    if (isArray(data) || data.children) {
      return false;
    }
    return true;
  }
};
const loadNode = (node: Node, resolve: (data: Tree[]) => void) => {
  getDemoList<Tree[]>({ name: node.label }).then(res => {
    resolve(res.data);
  });
};
const handleNodeClick = (data: Tree) => {
  if (data.code) {
    getChartList<ChartType>({
      code: data.code
    }).then(res => {
      chartData.value = res.data;
    });
  }
};

const handleClick = (tab, event) => {
  console.log(tab, event);
};

const router = useRouter();

onMounted(() => {
  getDemoList<Tree[]>().then(res => {
    treeData.value = res.data;
  });
});
</script>

<template>
  <div class="w-full h-full text-center p-2 bg-gray-100">
    <div class="border border-solid border-gray-200 h-full flex">
      <resize-box
        v-model:width="state.width"
        :directions="['right']"
        class="h-full min-w-[300px] max-w-[50%] text-center"
      >
        <div class="w-full h-full bg-white">
          <div class="px-2">
            <el-tabs v-model="state.active" @tab-click="handleClick">
              <el-tab-pane label="系统数据" name="first"
                ><div v-if="treeData.length">
                  <el-tree
                    :data="treeData"
                    :props="defaultProps"
                    :load="loadNode"
                    lazy
                    @node-click="handleNodeClick"
                  /></div
              ></el-tab-pane>
            </el-tabs>
          </div>
        </div>
      </resize-box>
      <div class="flex flex-1">
        <div class="flex-1">
          <div class="px-2">
            <el-tabs v-model="state.nav" @tab-click="handleClick">
              <el-tab-pane label="指标" name="first">指标</el-tab-pane>
              <el-tab-pane label="数据" name="second">数据</el-tab-pane>
              <el-tab-pane label="图形" name="third">图形</el-tab-pane>
            </el-tabs>
          </div>
        </div>
        <div class="w-[300px] h-full bg-violet-200" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped></style>
